package com.bpack;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.bpack.LoginActivity.RequestMethod;
import com.rest.BpackConnect;
import com.rest.JsonParse;
import android.app.ExpandableListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.Toast;
import android.widget.ExpandableListView.OnChildClickListener;
import android.widget.SimpleExpandableListAdapter;

/**
 * ToDoList
 * Displays the To Do list seen in the home screen of the b-pack app.
 * Items are organized by category that open to display a collapsable items list.
 * @author Daranhdara Hun
 */
public class ToDoList extends ExpandableListActivity implements OnChildClickListener {
	private static final String LABEL = "LABEL";
	private static final String VALUE = "VALUE";
	private JsonParse parser = new JsonParse();
	private ExpandableListAdapter mAdapter;
	
	/**
	 * @see android.app.Activity#onCreate(Bundle)
	 */
	//@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		/*
		 *  Instantiate the http client to the b-pack app.
		 *  NOTE: The token is hardcoded here but will need to be dynamic as app progresses 
		 */
		BpackConnect client = new BpackConnect("https://p2p.b-pack.com/a/endemo/api3/summary/todo?oauth_token=621d0e5a6039e393dd045cfd437aa276");
		try{
			client.Execute(RequestMethod.GET);
			parser.parseMachine(client.getResponse());
		} catch (Exception e) {
			e.printStackTrace();
			Toast.makeText(this, "Connection to b-pack app has failed", 1);
		}

		// Create a count of the list of ToDo Categories
		int listCnt = parser.getToDoCategories().length();
		
		/*
		 *  Decoded is the full list of translated templates with the appropriate values.
		 *  This list is category independent meaning decoded has everything. 
		 *  They are grouped by category though. Items in each category can be found by counting out
		 *  the number of items in each category. This is done in the following for loop.
		 */
		ArrayList<String> decoded = parser.templateProcessor();

		// Arraylist of the mappings of the parentData/parentData
		List<Map<String, String>> parentData = new ArrayList<Map<String, String>>();
		
		// Child Todo items
		List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
		int currentIndex = 0;
		
		// Populate the list with the text from decoded
		for (int i = 0; i < listCnt; i++) {
			// Current Todo map
			Map<String, String> curGroupMap = new HashMap<String, String>();
			// add the newly instantiated hashmap into the arraylist
			parentData.add(curGroupMap);
			// Map the LABEL to the name
			curGroupMap.put(LABEL, parser.getCatName(i));
			
			// Load number of child items under an ith ToDo item
			int childCnt = parser.getCatCount(i);
			int max = currentIndex + childCnt;
			
			// Intermediate list for group of child items
			List<Map<String, String>> children = new ArrayList<Map<String, String>>();
			
			for (int j = currentIndex; j < max; j++) {
				// Current HashMap for child
				Map<String, String> curChildMap = new HashMap<String, String>();
				// Add the HashMap to the arraylist
				children.add(curChildMap);
				// Map the label to the value
				curChildMap.put(LABEL, decoded.get(j));
			}
			
			// update currentindex to start at new point
			currentIndex += childCnt;
			childData.add(children);
		}

		/*
		 * An adapter that links a ExpandableListView with the underlying data. The implementation 
		 * of this interface will provide access to the data of the children (categorized by groups), 
		 * and also instantiate Views for children and groups.
		 */
		mAdapter = new SimpleExpandableListAdapter(
			this,
			parentData,
			R.layout.todo,
			new String[] { LABEL, VALUE },
			new int[] { android.R.id.text1, android.R.id.text2 },
			childData,
			R.layout.todochild,
			new String[] { LABEL, VALUE },
			new int[] { android.R.id.text1, android.R.id.text2 }
		);
		setListAdapter(mAdapter);
		
		// Must subscribe to the listener first
		getExpandableListView().setOnChildClickListener(this);
	}

	/**
	 * onChildClick
	 * Handles the click of a To Do item. 
	 * -Instantiates the intent and adds the location of the child item for the ItemInfo class.
	 * @return true.
	 */
	@Override
	public boolean onChildClick(ExpandableListView parent, View v, int groupPos, int childPos, long id){
		
		Intent intent = new Intent(ToDoList.this,ItemInfo.class);
		// Gets the id of the child item clicked 
		String childID = parser.getChildString(groupPos, childPos, parser.getRKey(groupPos));
		// intent.putExtra("id", childID);
		// intent.putExtra("key", parser.getRKey(groupPos));
		String uri = "https://p2p.b-pack.com/a/endemo/api3/data/" + parser.getRKey(groupPos) + "/" + childID + "?oauth_token=621d0e5a6039e393dd045cfd437aa276";
		intent.putExtra("uri", uri);
		startActivity(intent);
		return true;
		
	}	
}


